<template>
  <div class="login-container">
    <div class="login-form">
      <img src="@/assets/image/logo.595745bd.png" class="logo-lg" alt="">
      <el-form ref="from" class="el-from el-from--label-left" :model="ruleForm" :rules="rules">
        <!-- 手机号 -->
        <el-form-item prop="loginName">
          <!-- 小图标 -->
          <span class="likede likede-shouji" />
          <el-input v-model="ruleForm.loginName" placeholder="请输入账号" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <!-- 小图标 -->
          <span class="likede likede-mima" />
          <el-input ref="input" v-model="ruleForm.password" :type="type" placeholder="请输入密码" @keyup.enter="login" />
          <span class="likede" :class="type === 'password' ? 'likede-biyan': 'likede-zhengyan'" style="padding-left: 78px;" @click="changeicon" />
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <!-- 小图标 -->
          <span class="likede likede-anquan" />
          <el-input v-model="ruleForm.code" placeholder="请输入验证码" @keyup.enter="login" />
          <div style="display: inline-block;width:22%;">
            <img style="position: absolute;top:-1px;right: -1px; height: 103%;" :src="dynamicImage" alt="" @click="chanagImg">
          </div>
        </el-form-item>
        <!-- 登录 -->
        <el-button
          v-loading="loading"
          class="button"
          @click="login"
        >
          <span>登录</span>
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import debounce from '@/utils/throttle'
import { imagCodeApi } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      // 验证规则
      rules: {
        // 手机号
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        // 密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        // 验证码
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '请正确输入验证码', trigger: 'blur' }
        ]
      },
      ruleForm: {
        loginName: '',
        password: '',
        code: ''
      },
      type: 'password',
      dynamicImage: '',
      loading: false
    }
  },
  watch: {
  },
  mounted() {
    this.chanagImg()
  },
  methods: {
    // 点击显示关闭密码
    changeicon() {
      this.type = this.type === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    // 获取验证码图片
    async chanagImg() {
      const { data } = await imagCodeApi(666)
      const blob = new Blob([data]) // 返回的文件流数据
      const url = window.URL.createObjectURL(blob) // 将他转化为路径
      this.dynamicImage = url
    },
    // 点击登录
    login() {
      this.$refs.from.validate(async(val) => {
        if (val) {
          this.loading = true
          debounce(async() => {
            try {
              await this.$store.dispatch('user/GetTokenLogin', { ...this.ruleForm, loginType: 0, clientToken: 666 })
              this.$router.push({
                name: 'Dashboard'
              })
            } catch (error) {
              // 失败以后进行提示
              this.$message({
                showClose: true,
                message: error,
                type: 'error'
              })
            } finally {
              this.loading = false
            }
          }, 300)
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
.el-loading-mask {
  top: -1px;
    height: 102%;
    width: 101%;
}
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.el-input{
  width: 70% !important;
  color: #999 !important;
}
input{
  color: #999 !important;
  caret-color:unset !important;
}
/* reset element-ui css */
.login-container {
  .likede{
    padding-left: 10px;
  }
  background-image: url('~@/assets/image/background.be4fae7d.png') !important;
  background-position:center ;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .button{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
  .el-form-item {
    border: 1px solid #e2e2e2;
    border-radius: 5px;
    color: #454545;
  }
}
.el-form-item__content{
  line-height: 40px;
    position: relative;
    font-size: 14px;
    background-color: transparent;
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login-form{
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    .logo-lg{
      position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
    }
  }
}
</style>
